<?php
/**
 * Created by PhpStorm.
 * UsersF: Mloong
 * Date: 2019/3/2
 * Time: 14:26
 */

namespace app\sign\service;


use app\sign\model\Account;
use app\sign\model\Registered;
use think\facade\Session;

class showService
{
    private $from; # 推广方
    private $sign; # 标识码
    public function __construct()
    {
        $this->sign = Session::get('sign','sign_session');
        $this->from = Session::get('from','sign_session');
    }

    # 返回查找的sign
    public function returnSign($model_str,$page,$limit)
    {
        $classModel = 'app\\sign\\model\\'.$model_str;
        $model = new $classModel;
        $data = $this->getSignData($model,$page,$limit);
        return $data;
    }
    # 从缓存(session)中获取标识码  再从数据库查出所有标识码对应数据
    private function getSignData($model, $page,$limit)
    {
        $where = [
            ['from', '=', $this->from],
            ['sign', '=', $this->sign],
            ['status', '=', 1]
        ];
        $page = new \Page\Page($model, $page, $limit, [], [], $where);
        $result = $page->layuiPage();
        return $result;
    }

    public function getAllRegistered($page,$limit)
    {
        $where = [
            ['from', '=', $this->from],
            ['sign', '=', $this->sign],
            ['status', '=', 1],
            ['first', '=', 1]
        ];
        $model = new Registered();
        $page = new \Page\Page($model, $page, $limit, [], [], $where);
        $result = $page->layuiPage();
        return $result;
    }

    # 查找
    public function findTimeSign($page, $limit,$model_str,$filed)
    {
        $data = $this->getTimeSignData($page, $limit,$model_str,$filed);
        return $data;
    }
    # 按照时间查找对应的标识码的数据
    private function getTimeSignData($page, $limit,$model_str,$filed)
    {
        $time = $filed['time'];
        if(!strstr($time,'.')){
            $time_start = '';
            $time_end = '';
        }else {
            $date = explode('.', $filed['time']);
            $time_start = $date[0];
            $time_end = $date[1];
        }

        $where = [
            ['add_time', 'between time', [$time_start, $time_end]],
            ['from', '=', $this->from],
            ['sign', '=', $this->sign],
            ['first', '=', 1]
        ];

        if($model_str == 'Popularize'){
            unset($where['3']);
        }

        $classModel = 'app\\login\\model\\'.$model_str;
        $model = new $classModel;
        $page = new \Page\Page($model, $page, $limit, [], ['add_time' => 'desc'], $where);
        $result = $page->layuiPage();
        return $result;
    }
    # 删除一条记录
    public function delOne($model_str, $add_time)
    {
        $classModel = 'app\\login\\model\\'.$model_str;
        $model = new $classModel;
        return $model->where('from','=',$this->from)
            ->where('sign','=',$this->sign)
            ->where('add_time','=',$add_time)
            ->data(['status'=>-1])
            ->update();
    }
    # 获取成功贷款的所以记录
    public function getSuccessMoney($page,$limit)
    {
        $model = new Registered();
        $where = [
            ['from', '=', $this->from],
            ['sign', '=', $this->sign],
            ['loan_status', 'in', [2, 3, 4, 6, 8, 9]]
        ];
        $page = new \Page\Page($model, $page, $limit, [], [], $where);
        $result = $page->layuiPage();
        return $result;
    }
}